package com.example.utils;

import io.jsonwebtoken.SignatureAlgorithm;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;

import java.util.Date;

public class JWTUtils {

    public static final String jwtSecret = "esTfviPJpayDJvULBGDuJTDGUQefFNOakVnAisJoeSuDIiWAYasEqNKvdcAAAFRubkTMkuIAckxFStAPcIzsPUmaNabxDeAaWkfZdtoVDMvAfjOyzFuWLGzQErmaBXfBrJCTdGkbVxeOkyIJGkLTPoiPFwyjgScetlZOsToZsWyqIusnySkEmYugbDjmqgEJmAwexhrzQCfDRuePOZZPWUbqsdJWbCEwxwFMmkfnokBzxsyXCHoRYQNFWYfQkYNTvkyPpzRWAShxTsboLjRZcPQfhWfRESgMbAdhGEpNGJOLXvaanTNivEFvriIchetAEJrqOBfTrqllWgQuJOUEJuwXABarJtiyKmlTAeXkVqEfbCulZpCeCDRMDctjqtwhsBdKKMkHiWkcSUYsuJesXQbjoQRcaMExXGnDJGruRagTgVsLCupIhMCFQdQHbXxSPGMfzrCdDKhNHdHSgrFEzhrOVwvQahMMrPCsouDIcUhtDhRqQaIDWZGiYhzodhqr" +
            "DXshOlisxZdSwHCuBkfHTryECotKUKxYFYvmgKqffiMycmjlUSMqJjUTeqlPWzyplAgOibBAckxHSszGYxtXFVcLjSCCdoTUWKKYgASlhmVPfEiYnxnwBihcTLwDHoHfoOBqFJtBWXmmLHkpgeYyZWGFRHQzfBSZUIjGlzSjvCGdnVoqdGpIBigLLkYoqhymImKYNsPEvXMsPmRTtPUonrCcoYZiiTWMtsDlFElLCAMbvFamPcfcMHCuhibRiMQPOOWHRYxMeMpiqpyorDaAkUylJZFBNBFbzbeeCDMLOVdZObtMuuwvHJMLGDvJDnaUBeyIAoMRkJAifugopJiagvlYDsFurmzJStAXEHFpbgplTFDmWqkaIXDBcGftSotnrCRgqZMDjBmCKyJMsBRJNiNTqrIWLVGjDViFBIasVDllZyFzTYKzUXWpjeZGquRpsVjApMpydhhLzwjCIxvKuUnxybAtJcPjAKpzusYfMdIyGGDNaftlWGcfbEBtpnXh";

    /****
     * 创建TOKEN
     * @param userId
     * @return
     */
    public static String createToken(String userId) {
        Date expireTime = new Date(System.currentTimeMillis() + 60 * 60 * 1000);
        return Jwts.builder().setId(userId).setExpiration(expireTime).signWith(SignatureAlgorithm.HS512, jwtSecret).compact();
    }


    /***
     *
     * 解析TOKEN内容
     * @param token
     * @return
     */
    public static Claims parseToken(String token) {
        return Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token).getBody();
    }

    /***
     * 校验TOKEN
     * @param token
     * @return
     */
    public static boolean verifyToken(String token) {
        try {
            Claims claims = Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token).getBody();
            return !claims.getExpiration().before(new Date());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
